package com.hangzhou.xyj.ui.activity;

import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.model.SingModel;
import com.hangzhou.xyj.model.SingPageInfoModel;
import com.hangzhou.xyj.net.handler.GetSingByConditionHandler;
import com.hangzhou.xyj.net.http.HttpLoader;
import com.hangzhou.xyj.net.request.GetSingByConditionReq;
import com.hangzhou.xyj.ui.activity.base.BaseActivity;
import com.hangzhou.xyj.ui.adapter.SingingListAdapter;
import com.hangzhou.xyj.ui.adapter.base.BaseLoadingAdapter;
import com.hangzhou.xyj.utils.StringUtil;
import com.hangzhou.xyj.utils.constants.Constants;
import com.hangzhou.xyj.utils.constants.DataKey;

import java.util.List;

/**
 * Created by yaohuix on 2016/1/28  13:44
 * <p/>
 * 说明：更多作品页面
 */
public class MoreWorksActivity extends BaseActivity {
    private RecyclerView mRecyclerView;
    private String mtitle;//标题
    private int mUserId;//用户id
    private SingingListAdapter mAdapter;
    private int mIntentTag;

    @Override
    public void initDatas() {
        getUserShowList(1);
    }

    @Override
    public void initIntentData(Intent intent) {
        mtitle = intent.getStringExtra(DataKey.TITLE);
        mUserId = intent.getIntExtra(DataKey.INDEX, -1);
        mIntentTag = intent.getIntExtra(DataKey.INTENT_TAG, -1);
    }

    @Override
    public void initLayouts() {
        setContentView(R.layout.activity_more_works);
    }

    @Override
    public void initListeners() {
        mAdapter.setOnLoadingListener(new BaseLoadingAdapter.OnLoadingListener() {
            @Override
            public void loading() {
                mRecyclerView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        getUserShowList(mAdapter.getCurrentPage());
                    }
                }, 800);
            }
        });
    }

    @Override
    public void initTop() {
        if (StringUtil.isNotEmpty(mtitle)) {
            enableTitle(true, mtitle);
        } else {
            enableTitle(true, "标题");
        }
    }

    @Override
    public void initViews() {
        mRecyclerView = (RecyclerView) findViewById(R.id.rv_content);
        mRecyclerView.setAdapter(mAdapter = new SingingListAdapter(mRecyclerView, null));
        mAdapter.setIntentTag(mIntentTag);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
    }

    public void getUserShowList(final int currentPage) {
        GetSingByConditionReq getSingByConditionReq = new GetSingByConditionReq(GetSingByConditionReq.EParmType.TYPE_BY_USER_ID,
                mUserId, currentPage, Constants.PAGE_SIZE);
        HttpLoader.getInstance().sendRequest(new GetSingByConditionHandler(getSingByConditionReq, new MyHttpResponseListener() {
            @Override
            public void onSuccess(Object model) {
                super.onSuccess(model);
                List<SingModel> singList;
                singList = ((SingPageInfoModel) model).getSingList();
                if (currentPage == 1 && singList.isEmpty()) {
                    setNoDataTip(true, "暂时没有更多作品哦~");
                } else if (currentPage == 1) {
                    mAdapter.load(singList);
                } else {
                    mAdapter.update(singList);
                }
            }
            @Override
            public void onFail(String msg) {
                super.onFail(msg);
                mAdapter.setLoadingNoMore();//加载失败的时候
            }

            @Override
            public void onError() {
                super.onError();
                mAdapter.setLoadingNoMore();//加载失败的时候
            }
        }, 0));
    }
}
